package cs.upt.awp.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;

import cs.upt.awp.data.Country;

public class CountryDAO {

	@Autowired
	SessionFactory sessionFactory;

	Session session = null;
	Transaction tx = null;    
	
	
	public Country getEntityById(String id) throws Exception {
		session = sessionFactory.openSession();
		Country country = (Country) session.load(Country.class, id);
		tx = session.getTransaction();
		session.beginTransaction();
		tx.commit();
		
		return country;
	}

	@SuppressWarnings("unchecked")	
	public List<Country> getEntityList() throws Exception {
		session = sessionFactory.openSession();
		tx = session.beginTransaction();
		List<Country> countries = session.createCriteria(
				Country.class).addOrder(Order.asc("name")).list();
		tx.commit();
		session.close();
		return countries;
	}
	
}
